package com.dfe.backend.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 【描述】：
 *
 * @author LiChang
 * @since 2025-8-15 10:37
 */
public class JsonValidator {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    // 判断是否为有效的 JSON（对象或数组）
    public static boolean isJsonValid(String content) {
        if (content == null || content.trim().isEmpty()) {
            return false;
        }
        try {
            OBJECT_MAPPER.readTree(content);
            return true;
        } catch (JsonProcessingException e) {
            return false;
        }
    }

    public static void main(String[] args) {
        String validJson = "{\"name\":\"John\", \"age\":30}";
        String invalidJson = "{name: \"John\"}";

        System.out.println("Is valid JSON: " + isJsonValid(validJson));  // true
        System.out.println("Is valid JSON: " + isJsonValid(invalidJson)); // false
    }

}
